<?php
/**
 * Created by PhpStorm.
 * User: mt
 * Date: 2018/6/5
 * Time: 16:15
 */

namespace App\Http\Controllers\Api;


use App\Handlers\CounterHandler;
use App\Http\Controllers\Controller;
use App\Models\Gamelog;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class GamesController extends Controller
{
    private $game_type = [
        "typhoon",//气象灾害-台风
        "earthquake",//地震
        "etiquette", //八礼
        "fire",     //火灾
        "aredu"  //AR教育
    ];

    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'student_id' => 'required|integer|min:1',
            'school_id' => 'required|integer|min:1',
            'game_type' => 'required|in:' . implode(",", $this->game_type),
            'game_data' => 'required|array',
        ]);
        if ($validator->fails()) {
            return response()->json(['msg' => '参数错误'])->setStatusCode(400);
        }
        $gamelog = new Gamelog();
        $gamelog->student_id = $request->student_id;
        $gamelog->school_id = $request->school_id;
        $gamelog->game_type = $request->game_type;
        $gamelog->game_data = json_encode($request->game_data);

        $gamelog->save();
        $res = app(CounterHandler::class)->handleData($request->game_data, $request->game_type);
        return response()->json($res)->setStatusCode(201);
    }

    //某个题库中某个题目的正确率及选择信息
    public function getGameData()
    {
        $type = 'typhoon';
        $num = 1;
        $data = Gamelog::where('game_type', $type)->get(['game_data']);

    }
}